package com.authine.cloudpivot.ext.vo;

import com.alibaba.fastjson.JSONObject;
import com.authine.cloudpivot.ext.utils.Utils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 物流单
 *
 * @author 张天赐
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ApiModel("物流单")
public class ApiLogisticsInfo implements Serializable {


    private static final long serialVersionUID = -2929898517352823501L;

    @ApiModelProperty(value = "物流单号", dataType = "string")
    private String mailNo;

    @ApiModelProperty(value = "提示信息，用于提醒用户可能出现的情况", dataType = "string")
    private String upgradeInfo;

    @ApiModelProperty(value = "返回提示信息", example = "查询成功 或 未查到单号对应的快递公司,请指定快递公司编码", dataType = "string")
    private String msg;

    @ApiModelProperty(value = "待查询、查询异常、暂无记录、在途中、派送中、已签收、用户拒签、疑难件、无效单、超时单、签收失败、退回", dataType = "string")
    private String status;

    @ApiModelProperty(value = "快递公司电话", example = "95311", dataType = "string")
    private String tel;

    @ApiModelProperty(value = "物流详情", example = "[{'time':'2021-03-10 00:00:00','context':'快件离开【上海】 已发往 【南通】'}]", dataType = "list")
    private List<ApiLogisticsInfoDetail> details;

    /**
     * 根据查询的物流信息 封装返回结果
     * @param logicDetailJsonData 物流信息集合
     * @return
     */
    public static List<ApiLogisticsInfo> getListBylogicDetail(List<JSONObject> logicDetailJsonData) {

        List<ApiLogisticsInfo> results = new ArrayList<>();
        for (JSONObject logicDetailInfo : logicDetailJsonData) {

            if (null == logicDetailInfo || null == logicDetailInfo.get("showapi_res_body")) {
                continue;
            }

            JSONObject body = logicDetailInfo.getJSONObject("showapi_res_body");
            ApiLogisticsInfo apiLogisticsInfo = ApiLogisticsInfo.builder().build();
            apiLogisticsInfo.setTel(body.getString("tel"));
            apiLogisticsInfo.setMsg(body.getString("msg"));
            apiLogisticsInfo.setUpgradeInfo(body.getString("upgrade_info"));
            apiLogisticsInfo.setMailNo(body.getString("mailNo"));
            apiLogisticsInfo.setStatus(logisticsStatusToStr(body.getInteger("status")));

            List<ApiLogisticsInfoDetail> detailList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(body.getJSONArray("data"))) {
                for (int i = 0; i < body.getJSONArray("data").size(); i++) {
                    JSONObject data = body.getJSONArray("data").getJSONObject(i);
                    ApiLogisticsInfoDetail detail = ApiLogisticsInfoDetail.builder().build();
                    detail.setContext(data.getString("context"));
                    detail.setTime(Utils.strToDate(data.getString("time")));
                    detailList.add(detail);
                }
            }
            apiLogisticsInfo.setDetails(detailList);
            results.add(apiLogisticsInfo);
        }
        return results;
    }

    public static String logisticsStatusToStr(Integer status) {
        if (null == status) {
            return null;
        }
        switch (status) {
            case -1:
                return "待查询";
            case 1:
                return "暂无记录";
            case 2:
                return "在途中";
            case 3:
                return "派送中";
            case 4:
                return "已签收";
            case 5:
                return "用户拒签";
            case 6:
                return "疑难件";
            case 7:
                return "无效单";
            case 8:
                return "超时单";
            case 9:
                return "签收失败";
            case 10:
                return "退回";
            default:
                return "";
        }
    }
}
